package com.yb0os1.controller.user;

import com.yb0os1.constant.JwtClaimsConstant;
import com.yb0os1.dto.UserLoginDTO;
import com.yb0os1.entity.User;
import com.yb0os1.properties.JwtProperties;
import com.yb0os1.result.Result;
import com.yb0os1.serve.UserServe;
import com.yb0os1.utils.JwtUtil;
import com.yb0os1.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
@Tag(name = "C端用户相关接口")
@Slf4j
public class UserController {
    @Autowired
    private UserServe userServe;
    @Autowired
    private JwtProperties jwtProperties;
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("微信登录......");
        // 微信登录
        User user =  userServe.wxlogin(userLoginDTO);

        //为微信用户生成jwt令牌
        Map<String,Object>claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token =  JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);
        return Result.success(UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build());
    }
}
